package ModelLayer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.HashMap;
import java.util.Date;

public class Order
{ 
    private HashMap<Integer,LineItem> products;
    private ArrayList<Payment> payments;
    private static double maxdisc=20;
    private double custdisc,pickdisc,quantitydisc,totaldisc,total,totalfinal,paid;
    private Customer customer;
    private Staff employee,paymentStaff;
    private int id;
    private Date startDate,endDate;
    private String state; // pending, cancel, success

    public Order(int id,Customer customer, Staff staff)
    {   
        this.id=id;
        products=new HashMap<Integer,LineItem>();
        payments=new ArrayList<Payment>();
        this.customer=customer;
        startDate=new Date();
        endDate=startDate;
        state="pending";
        employee = staff;
        paymentStaff = staff;
        custdisc=0;
        pickdisc=0;
        quantitydisc=0;
        totaldisc=0;
        total=0;
        totalfinal=0;
        paid=0.00;
        setCustomerDiscount();
    }

    public int addProduct(int idProd, int quantity)
    {
        LineItem newLineItem = new LineItem(idProd, quantity);
        if(newLineItem.availProd())
        {
            products.put((Integer)idProd,newLineItem);
            total = total + newLineItem.getSubtotal();
            return 1;
        }
        
        return -1;
    }
    
    public void removeProduct(int idP)
    {
        if(products.containsKey(idP))
        {
            total = total - products.get(idP).getSubtotal();
            products.remove(idP);
        }
    }
    
    public void updateQuantity(int idP, int quantity)
    {
        if(products.containsKey(idP))
        {
            products.get(idP).setQuantity(quantity);
            total = total - products.get(idP).getSubtotal();
        }
    }

    public void addPayment(Payment newPayment) { payments.add(newPayment); }
    
    public void updatePaid(double val) { paid += val; }
    
    public void setPaid(double val) { paid = val; }

    public void setCustomer(Customer newCustomer)  {customer=newCustomer;}

    public void setEmployee(Staff newEmployee) {employee=newEmployee;}

    public void setCustomerDiscount()  {custdisc=CustomerDiscount.getDiscount(customer.getType());}

    public void setPickupDiscount(String check) {pickdisc=PickupDiscount.getDiscount(check);}

    public static void setMaximumDiscount(double newmaxdisc)  {maxdisc=newmaxdisc;}

    public void setQuantityDiscount(double disc) {quantitydisc=disc;}

    public void updateTotal()
    {
        totaldisc = pickdisc+quantitydisc+custdisc;
        if(totaldisc>maxdisc) {totaldisc=maxdisc;}
        totalfinal=total-total*(totaldisc/100);
    }

    public void setEndDate(Date endDate) { this.endDate = endDate;}

    public void expiration()  {if (endDate.getTime()<(new Date()).getTime()) {setState("canceled");}}

    public void setState(String state) {this.state=state;
                                        if (state.equals("completed")) {endDate=new Date();
                                                                        employee.ordersSold();}
                                        if (state.equals("canceled")) {endDate=new Date();}}
    
    public void setPaymentStaff(Staff newStaff) { this.paymentStaff = newStaff; }
    
    

    public int getId() {return id;}
    
    public Date getStartDate() {return startDate;}
    public Date getEndDate() {return endDate;}

    public Staff getPaymentStaff() {   return paymentStaff; }
    
    public Staff getEmployee() {   return employee; }
    
    public Customer getCustomer() { return customer; }
    
    public String getCustomerName() { return customer.getName(); }
    public String getStaffName() { return employee.getName(); }
    public String getPaymentStaffName() { return paymentStaff.getName(); }
    public String getState() { return state; }
    
    public double getDiscount() { return totaldisc; }
    public double getTotal()  { return total; }
    public double getFinalTotal() { return totalfinal; }
    public double getCustomerDiscount() { return custdisc; }
    public double getDeliveryDiscount() { return pickdisc; }
    public double getQuantityDiscount() { return quantitydisc; }
    public double getPaidAmount() { return paid; }
    
    public double getChange() { return paid-total;}
    
    public Iterator getProducts() {  return products.values().iterator(); }
}  